package com.tanhua.server.controller;


import com.tanhua.dubbo.api.UserApi;

import com.tanhua.server.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class LoginController {


    @Autowired
    private UserService userService;

    @DubboReference
    private UserApi userApi;

    /**
     * 发送短信验证码
     *      请求方式：post
     *      请求路径：/login
     *      参数：Map集合 @RequestBody
     *      返回值：void  ：需要通过ResponseEntity封装
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map map) {
        //1、获取手机号码
        String phone = (String) map.get("phone");
        //2、发送验证码
        userService.login(phone);
        //3、返回
        return ResponseEntity.ok(null);
    }

    /**
     * 用户登录：
     *    请求方式：Post
     *    请求地址：/loginVerification
     *    请求参数：phone，verificationCode  （@RequestBody）
     *    返回值：
     *       token，isNew
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map map) {
        //1、获取请求参数
        String phone = (String) map.get("phone");
        String code = (String) map.get("verificationCode");
        //2、调用Service登录，返回Map集合（token，isNew）
        Map retMap = userService.loginVerification(phone,code);
        //3、返回
        return ResponseEntity.ok(retMap);
    }

//    /**
//     * 根据手机号码查询用户
//     */
//    @GetMapping("/{mobile}")
//    public ResponseEntity findByMobile(@PathVariable("mobile") String mobile) {
//        User user = userService.findByMobile(mobile);
//        return ResponseEntity.ok(user);
//    }
}
